<template>
    <div>
        <el-table v-if="gridList"  height="calc(100vh - 280px)" :data="gridList" style="width: 100%;" stripe>
            <el-table-column prop="pointName" label="测点名称"></el-table-column>
            <el-table-column prop="manuId" label="传感器编号"  sortable></el-table-column>
            <el-table-column prop="data_time" label="时间"  sortable></el-table-column>
            <el-table-column prop="data" v-for="(item,idx) in titleList" :key="idx"> 
                <template slot="header" slot-scope="scope" >
                    {{item.phyName}}
                </template>
                <template  slot-scope="scope">
                    <span >{{(scope.row['data'+item.lastIdx]==undefined?'':scope.row['data'+item.lastIdx])+(scope.row['unit'+item.lastIdx]||"")}}</span>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-if="gridList" class="page_sub" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page"
        :page-sizes="[60,120,200,300,400,500]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
        </el-pagination>
    </div>
</template>
<script>
import { util } from "@/utils/common.js";
export default {
    data(){
        return{
            gridList:null,
            titleList:[],
            page:1,
            size:60,
            total:0,
        }
    },
    props:['searchFactor','pointList'],
    created(){
        this.render();
    },
    methods:{
        handleSizeChange(size){
            this.size = size;
            this.page = 1;
            this.render(this.page)
        },
        handleCurrentChange(page){
            this.page = page;
            this.size = this.size;
            this.render(this.page)
        },
        render(page){
            this.page = page||1
            let searchFactor = this.searchFactor,dateTime = ""
            let startTime = searchFactor.modify_start_time||"",
                endTime = searchFactor.modify_end_time||""
            if(startTime){
                startTime = startTime.split(" ").length>1?startTime.replace(' ','T'):(startTime+"T00:00:00")
            }else{
                startTime = '*'
            }
            if(endTime){
                endTime = endTime.split(" ").length>1?endTime.replace(' ','T'):(endTime+"T23:59:59")
            }else{
                endTime = '*'
            }
            if(startTime=="*"&&endTime=="*"){
				dateTime = ""
			}else{
				dateTime = " AND data_time:["+startTime+" TO "+endTime+"]"
			}
            if(this.pointList.length ==0){
                this.gridList = []
                return
            }
            let query = '',pointObj={}
            this.pointList.forEach(item=>{
                if(!query){
                    query+='data_id:'+item.deviceId
                }else{
                    query+=' OR data_id:'+item.deviceId
                }
                pointObj[item.deviceId] = item;
            })
            query= "("+query+")"+dateTime
            let sendData = {
                query:query,
                pageSize:this.size,
                pageNum:this.page
            }
            this.$httpEs.post('/tiesan/queryTiesanTableResult?cancelRequest=true',sendData).then(data=>{
                let titleList  = [];
                data.data.datas.forEach(item=>{
                    item.pointName =pointObj[item.data_id].pointName
                    Object.keys(item).forEach(it=>{
                        let lastIdx = it.slice(-1)
                        if(util.phyList.includes(it)&&titleList.findIndex(i=>{return i.lastIdx == lastIdx})==-1){
                            titleList.push({lastIdx:lastIdx,phyName:item['name'+lastIdx],unit:item['unit'+lastIdx]})
                        }
                    })
                })
                titleList = titleList.sort((a,b)=>{return a.lastIdx-b.lastIdx})
                this.titleList = [];
                this.$nextTick(()=>{
                    this.titleList = titleList
                })
                this.gridList = data.data.datas;
                this.total = data.data.total
            })
        }
    }
}
</script>